<?php

class CloudSyncController
{
    private $cloudSyncAttachmentModel;
    private $cloudSyncContentModel;
    private $userMemberModel;
    private $shopModel;

    function __construct()/*{{{*/
    {
        $this->cloudSyncAttachmentModel =   new CloudSyncAttachmentModel();
        $this->cloudSyncContentModel    =   new CloudSyncContentModel();
        $this->userMemberModel          =   new UserMemberModel();
        $this->shopModel                =   new ShopModel();
    }/*}}}*/

    public function cloudSync($cloudInfo)/*{{{*/
    {
        $remnant    =   $this->cloudTraffic($cloudInfo);
        if (empty($cloudInfo['uid']) || empty($cloudInfo['type']))
        {
            AC::$errorNo    =   'CSC001';
            return  array('result' => FALSE, 'traffic' => $remnant['traffic'], 'allTraffic' => $remnant['allTraffic']);
        }
        if (isset($cloudInfo['title'])){
            $cloudInfo['title'] = UK::getstr($cloudInfo['title'], 500, '', 1);
        }
        if (isset($cloudInfo['content'])){
            $cloudInfo['content'] = UK::getstr($cloudInfo['content'], 50000);
        }
        if (isset($cloudInfo['string6'])){
            $cloudInfo['string6'] = UK::getstr($cloudInfo['string6'], 50000);
        }
        if (empty($cloudInfo['title'])  &&  !isset($cloudInfo['content'])   &&  empty($cloudInfo['attachment']))
        {
            AC::$errorNo    =   'CSC002';
            return  array('result' => FALSE, 'traffic' => $remnant['traffic'], 'allTraffic' => $remnant['allTraffic']);
        }
        if (!empty($cloudInfo['traffic'])   &&  $cloudInfo['traffic'] > 0)
        {
            if ($cloudInfo['traffic']   >   $remnant['traffic'])
            {
                AC::$errorNo    =   'CSC005';
                return  array('result' => FALSE, 'traffic' => $remnant['traffic'], 'allTraffic' => $remnant['allTraffic']);
            }
            $remnant['traffic'] -=  $cloudInfo['traffic'];
            $allTraffic = $remnant['allTraffic'];
            unset($remnant['allTraffic']);
            $this->userMemberModel->setMemberInfo($remnant);
            $remnant['allTraffic'] = $allTraffic;

            $this->saveSyncLogByMonth($cloudInfo['uid'], $remnant['allTraffic'], $remnant['traffic']);
        }
        if (isset($cloudInfo['paper']) && !empty($cloudInfo['paper']) && $cloudInfo['type'] == 1 && empty(AC::$iosFullVersion)){
            $pattern = '/^[0-9]{5}/';
            if(preg_match($pattern, $cloudInfo['paper'])) {
                $pid = substr($cloudInfo['paper'], 0, 5);
                $paperUser = $this->shopModel->getPaperUser($cloudInfo['uid'], $pid);
                if (empty($paperUser)){
                    $cloudInfo['paper'] = '';
                }
            }else{
                $cloudInfo['paper'] = '';
            }
        }
        $cscId  =   $this->cloudSyncContentModel->cloudSync($cloudInfo);
        if (empty($cscId))
        {
            UK::varDump(AC::$errorNo, __METHOD__);
            return  array('result' => FALSE, 'traffic' => $remnant['traffic'], 'allTraffic' => $remnant['allTraffic']);
        }

        $cloudInfo['bodyId']    =   $this->cloudSyncContentModel->getBodyId($cloudInfo['uid'], $cscId);
        if (empty($cloudInfo['bodyId']))
        {
            UK::varDump(AC::$errorNo, __METHOD__);
            return  array('result' => FALSE, 'traffic' => $remnant['traffic'], 'allTraffic' => $remnant['allTraffic']);
        }

        if (isset($cloudInfo['attachment']) &&  count($cloudInfo['attachment']) >   0   &&  !empty($cloudInfo['attachment'][0]))
        {
            $tmpInfo    =   array(
                                'uid'           =>  $cloudInfo['uid'],
                                'type'          =>  $cloudInfo['type'],
                                'bodyId'        =>  $cloudInfo['bodyId'],
                                'attachment'    =>  $cloudInfo['attachment'],
                            );
            $this->cloudSyncAttachmentModel->cloudSync($tmpInfo);
        }

        return  array('id' => $cscId, 'traffic' => $remnant['traffic'], 'allTraffic' => $remnant['allTraffic']);
    }/*}}}*/

    /**
     * 按月记录流量变动log
     * @param $uid
     * @param $allTraffic
     * @param $traffic
     * @param bool $switch
     */
    private function saveSyncLogByMonth($uid, $allTraffic, $traffic, $switch = true) {
        $time      = AC::SERVER_TIME;
        $dayFormat = date("Y-m-d H:i:s", $time);
        $file_pre  = "/data0/logs/pink-sync/";
        $month     = date("Y-m", $time);
        $logName   = $file_pre.$month.".log";
        $content   = $dayFormat.": "."uid:".$uid." allTraffic:".$allTraffic." traffic:".$traffic.PHP_EOL;
        try{
            if ($switch && is_dir($file_pre)) {
                file_put_contents ($logName, $content, FILE_APPEND);
            }
        }catch (Exception $e){
        }
    }

    public function cloudTraffic($cloudInfo)/*{{{*/
    {
        if (empty($cloudInfo['uid']))
        {
            AC::$errorNo    =   'CSC004';
            return  FALSE;
        }

        $result =   $this->userMemberModel->getMemberInfo($cloudInfo);
        $allTraffic = $result['allTraffic'] + $result['buyTraffic'] + $result['vipTraffic'];
        $result['traffic']>$allTraffic && $result['traffic'] = $allTraffic;
        $traffic=   array('traffic' => $result['traffic'], 'uid' => $result['uid'], 'allTraffic' => $allTraffic );

        # new Api access
        if (isset($cloudInfo['saveTime']))
        {
            $traffic['cloudList']   =   $this->cloudSyncList($cloudInfo);
        }
        return  $traffic;
    }/*}}}*/

    public function cloudSyncCount($cloudInfo)/*{{{*/
    {
        if (empty($cloudInfo['uid']))
        {
            AC::$errorNo    =   'CSC004';
            return  FALSE;
        }

        $cloudList  =   $this->cloudSyncContentModel->cloudSyncCount($cloudInfo);
        return  $cloudList;
    }/*}}}*/

    public function cloudSyncList($cloudInfo)/*{{{*/
    {
        if (empty($cloudInfo['uid']))
        {
            AC::$errorNo    =   'CSC004';
            return  FALSE;
        }

        $cloudList  =   $this->cloudSyncContentModel->cloudSyncList($cloudInfo);
        return  $cloudList;
    }/*}}}*/

    public function cloudDownload($cloudInfo)/*{{{*/
    {
        if (empty($cloudInfo['uid']))
        {
            AC::$errorNo    =   'CSC004';
            return  FALSE;
        }

        $cloudList  =   $this->cloudSyncContentModel->cloudDownload($cloudInfo);

        if (empty($cloudList)   ||  !is_array($cloudList))  return  NULL;

        foreach ($cloudList as  &$cloudBody)
        {
            if ($cloudBody['type'] == 1){
                $paper = json_decode($cloudBody['string1'], true);
                $cloudBody['paper'] = strval($paper['paper']);
                $cloudBody['string1'] = '';
            }
            $cloudInfo['bodyId']    =   $this->cloudSyncContentModel->getBodyId($cloudInfo['uid'], $cloudBody['id']);
            if (empty($cloudInfo['bodyId']))    continue;

            $tmp    =   $this->cloudSyncAttachmentModel->getCloudAttachment($cloudInfo);
            if (empty($tmp))    continue;

            $cloudBody['attachment']    =   $tmp;
        }

        return  $cloudList;
    }/*}}}*/

    public function cloudUpdate($cloudInfo)/*{{{*/
    {
        $remnant    =   $this->cloudTraffic($cloudInfo);
        if (empty($cloudInfo['uid']) || empty($cloudInfo['type'])   ||  empty($cloudInfo['id']))
        {
            AC::$errorNo    =   'CSC003';
            return  array('result' => FALSE, 'traffic' => $remnant['traffic']);
        }
        if (empty($cloudInfo['title'])  &&  !isset($cloudInfo['content'])   &&  empty($cloudInfo['attachment']))
        {
            AC::$errorNo    =   'CSC002';
            return  array('result' => FALSE, 'traffic' => $remnant['traffic']);
        }
        if (isset($cloudInfo['title'])){
            $cloudInfo['title'] = UK::getstr($cloudInfo['title'], 500, '', 1);
        }
        if (isset($cloudInfo['content'])){
            $cloudInfo['content'] = UK::getstr($cloudInfo['content'], 50000);
        }
        if (isset($cloudInfo['string6'])){
            $cloudInfo['string6'] = UK::getstr($cloudInfo['string6'], 50000);
        }
        if (!empty($cloudInfo['traffic'])   &&  $cloudInfo['traffic'] > 0)
        {
            if ($cloudInfo['traffic']   >   $remnant['traffic'])
            {
                AC::$errorNo    =   'CSC005';
                return  array('result' => FALSE, 'traffic' => $remnant['traffic']);
            }
            $remnant['traffic'] -=  $cloudInfo['traffic'];
            $allTraffic = $remnant['allTraffic'];
            unset($remnant['allTraffic']);
            $this->userMemberModel->setMemberInfo($remnant);
            $remnant['allTraffic'] = $allTraffic;
        }
        $cloudInfo['bodyId']    =   $this->cloudSyncContentModel->getBodyId($cloudInfo['uid'], $cloudInfo['id']);

        $this->cloudSyncAttachmentModel->cloudUpdate($cloudInfo);
        $this->cloudSyncContentModel->cloudUpdate($cloudInfo);

        if (!empty(AC::$errorNo))
        {
            UK::varDump(AC::$errorNo, __METHOD__);
            return  FALSE;
        }
        return  array('result' => TRUE, 'traffic' => $remnant['traffic'], 'id' => intval($cloudInfo['id']));
    }/*}}}*/

    public function cloudRemove($cloudInfo)/*{{{*/
    {
        if (empty($cloudInfo['uid']) ||  (empty($cloudInfo['id']) && empty($cloudInfo['in'])))
        {
            AC::$errorNo    =   'CSC003';
            return  FALSE;
        }
        if (empty($cloudInfo['in']))
        {
            $cloudInfo['bodyId']    =   $this->cloudSyncContentModel->getBodyId($cloudInfo['uid'], $cloudInfo['id']);
        }
        else
        {
            $bodyIds=   array();
            $ids    =   explode(',', $cloudInfo['in']);
            foreach ($ids   as  $id)
            {
                if (empty($id)) continue;
                $bodyIds[]  =   $this->cloudSyncContentModel->getBodyId($cloudInfo['uid'], $id);
            }
            $cloudInfo['bodyIds']   =   implode(',', $bodyIds);
        }

        #   $this->cloudSyncAttachmentModel->cloudRemove($cloudInfo);
        $this->cloudSyncContentModel->cloudRemove($cloudInfo);

        if (!empty(AC::$errorNo))
        {
            UK::varDump(AC::$errorNo, __METHOD__);
            return  FALSE;
        }
        return  TRUE;
    }/*}}}*/

    public function cloudRemoveByUid($cloudInfo)/*{{{*/
    {
        if (empty($cloudInfo['uid']))
        {
            AC::$errorNo    =   'CSC006';
            return  FALSE;
        }

        $this->cloudSyncContentModel->cloudRemoveByUid($cloudInfo);

        if (!empty(AC::$errorNo))
        {
            return  FALSE;
        }
        return  TRUE;
    }/*}}}*/
    
    #恢复用户同步信息     /*{{{*/
    public function regainCloudSync($cloudInfo)
    {
        if (empty($cloudInfo['uid']))
        {
            AC::$errorNo    =   'CSC006';
            return  FALSE;
        }
        
        $this->cloudSyncContentModel->regainCloudSync($cloudInfo);
        
        if (!empty(AC::$errorNo))
        {
            return  FALSE;
        }
        return  TRUE;
    }/*}}}*/
}

